package com.icecola.d4_synchronized_code;

public class Account {
    private String cardId; //卡号
    private double money; //余额

    public String getCardId() {
        return cardId;
    }

    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }
    public void drawMoney(double money) {
        String name = Thread.currentThread().getName();
        // ctrl+alt+t 快捷键 选9 加锁
        // “关键词”做锁，全部是同一个锁，会影响无关线程执行
        // 当前对象作为锁
        synchronized (this) {
            if(this.money >= money){
                System.out.println("当前余额"+this.money);
                this.money -= money;
                System.out.println(name+"取钱成功，余额:"+this.money);
            }else {
                System.out.println(name+"余额不足");
            }
        }
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }


}
